|
STEP 14: Browser Compatibility: Using Music Object
|
In the previous step we saw how we could use an EMBED tag nested inside an OBJECT to achieve compatibiltiy with both Netscape Navigator and Microsoft Internet Explorer. While this does work reliably, one problem is that it is quite a bunch of HTML to type each time you wish to play some background music. There are quite a few attributes in the EMBED tag that must be echoed as parameters of the OBJECT tag, opening up the possibility of errors occurring after several generations of updates. Another problem is that using this approach is only really appropriate for background music and it doesn't scale well into the vast world of interactive possibilities offered through the Music Object API.
An easier way to embed background music in a way that can scale well into full-blown interactive sonification is to use the Music Object API's Music.magicEmbed static method. This method allows one to embed non-scriptable Player instances in a way that is compatible with both Navigator and Internet Explorer, without having to worry about the specific differences between the EMBED tag and the OBJECT tag.
The following before-and-after example shows how the cross-browser EMBED-OBJECT tag combination from the previous step in this tutorial is translated to use the Music.magicEmbed method.
| BEFORE (Using EMBED & OBJECT tags) |
<OBJECT SRC="trance.rmf" WIDTH=144 HEIGHT=60 CLASSID="CLSID:B384F118-18EE-11D1-95C8-00A024330339" CODEBASE="http://download.beatnik.com/beatnik-player/beatnik-player.cab" > <PARAM NAME="WIDTH" VALUE="144"> <PARAM NAME="HEIGHT" VALUE="60"> <PARAM NAME="DISPLAY" VALUE="SONG"> <PARAM NAME="AUTOSTART" VALUE="FALSE"> <PARAM NAME="LOOP" VALUE="FALSE"> <EMBED SRC="trance.rmf" WIDTH=144 HEIGHT=60 DISPLAY=SONG AUTOSTART=FALSE LOOP=FALSE TYPE="audio/rmf" PLUGINSPAGE="http://www.beatnik.com/to/?player" > <\OBJECT> |
| AFTER (Using the Music.magicEmbed Method) |
<SCRIPT SRC="music-object.js"></SCRIPT>
<SCRIPT LANGUAGE=JavaScript><!-- //
Music.magicEmbed (
'SRC="trance.rmf" ' +
'WIDTH=144 ' +
'HEIGHT=60 ' +
'DISPLAY=SONG ' +
'AUTOSTART=FALSE ' +
'LOOP=FALSE'
);
// --></SCRIPT>
|
In both the before and after stage in the above comparison, the text that is highlighted indicates the customizable attributes / parameters for the embedded Player instance. From the comparison, you will notice how much simpler the Music.magicEmbed approach is. Most notably, there is no need to duplicate values in both the EMBED and OBJECT tags. The Music.magicEmbed method also automatically takes care of the TYPE and PLUGINSPAGE attributes for the EMBED tag, as well as the CLASSID and CODEBASE attributes for the OBJECT tag, so these don't have to be specified when calling the Music.magicEmbed method.
It's important to note that you cannot use the Music.magicEmbed method unless you have first sourced in the "music-object.js" JavaScript file, since the method is defined inside this file. This is done with the statement <SCRIPT SRC="music-object.js"></SCRIPT>. In our example we're assuming that the "music-object.js" file exists inside the same folder as the HTML document that is using it. If the file was in a different folder, then we'd have to specify the correct relative path to it, just like any other Web site resource (such as a GIF or JPEG image file).